package zz.note.thread.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import zz.note.thread.entity.UserEntity;

import java.util.concurrent.Future;

/**
 * @Author: ZZ
 * @date 2022/2/16
 */
@Component
@Slf4j
public class LoginManage {
	@Async
	public void asynLogin(UserEntity userEntity) {
		loginLog(userEntity);
		sendSms(userEntity);
		sendEmail(userEntity);
	}

	private UserEntity dbLogin(UserEntity userEntity) {
		log.info(">>>1.正在查询数据库登陆<<<");
		try {
			Thread.sleep(2000);
		} catch (Exception e) {

		}
		return new UserEntity();
	}

	private void loginLog(UserEntity userEntity) {
		log.info(">>>2.异步开始写登陆的日志<<<");
		try {
			Thread.sleep(2000);
		} catch (Exception e) {

		}
	}

	private void sendSms(UserEntity userEntity) {
		log.info(">>>3.异步开始发送短信<<<");
		try {
			Thread.sleep(2000);
		} catch (Exception e) {

		}
	}

	private void sendEmail(UserEntity userEntity) {
		log.info(">>>4.异步开始发送邮件<<<");
		try {
			Thread.sleep(2000);
		} catch (Exception e) {

		}
	}


	/**
	 * 异步带线程返回结果
	 * @param userEntity
	 * @return
	 */
	@Async
	public Future<String> futureTaskLogin(UserEntity userEntity) {
		loginLog(userEntity);
		sendSms(userEntity);
		sendEmail(userEntity);
		return new AsyncResult<String>("异步发送短信完成");
	}

	public static void main(String[] args) {
		LoginManage loginManage = new LoginManage();
		loginManage.dbLogin(new UserEntity());
	}
}

